Repository navigation
fix: validate consecutive setFieldValue calls correctly - #4084
kanza-shahid-dev wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: 2208577 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
@kanza-shahid-dev is attempting to deploy a commit to the Formik Team on Vercel. A member of the Team first needs to authorize it. |
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
d944215 to
77a06eb
Compare
77a06eb to
2208577
Compare
agammann
left a comment
There was a problem hiding this comment.
The validation input now comes from stateRef.current.values, but functional updater values still read state.values from the last render. If setFieldValue('count', n => n + 1) is called twice before React rerenders, dispatch advances stateRef.current after the first call, yet the second updater still receives the original count; the field ends at 1 instead of 2. Could the updater read stateRef.current.values too, with a same-tick functional-update regression test? I reviewed the diff; I did not run tests locally.
Description
Fixes #3754
Fixes an issue where multiple consecutive
setFieldValuecalls could trigger validation using stale form values.When
setFieldValuewas called multiple times asynchronously, the validation triggered by a later call could use an outdatedstate.values. This could result in stale validation errors even though the form values had already been updated.Changes
stateRef.current.valueswhen handlingsetFieldValue.validateFormWithHighPriority.setFieldValuecalls with a Yup validation schema.Testing
yarn test Formik.test.tsx— 89 tests passedyarn test— 227 tests passedgit diff --cached --check— passed